package com.prodavalnik.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.prodavalnik.model.Category;
import com.prodavalnik.model.DataBaseController;

/**
 * Servlet implementation class getCategory
 */
@WebServlet("/getCategory")
public class getCategory extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public getCategory() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		List<Category> categories = null;
		if(name.equals("root")){
			categories  = DataBaseController.getAllRootCategories();
		}
		else
			categories = DataBaseController.getCategoriesByName(name);
		
		if(categories == null)
			return;
		
		PrintWriter pw = response.getWriter();
		pw.println("<ul>");
		for(int i = 0; i < categories.size();i++)
			pw.println("<li id='"+categories.get(i).getId()+"s'><a href=\"javascript:whichCategoryToSearch('"+categories.get(i).getName()+"')\">"+categories.get(i).getName()+"</a></li>");
		pw.println("</ul>");
		pw.println("!");
		pw.println("<ul>");
		for(int i = 0; i < categories.size();i++)
			pw.println("<li class=\"nav-header\" id='"+categories.get(i).getId()+"'><a href=\"javascript:nextSubCategory('"+categories.get(i).getId()+"','"+categories.get(i).getName()+"');\" data-toggle=\"collapse\" data-target=\"#userMenu\" class=\"collapsed\">"+categories.get(i).getName()+"</a></li>");
		pw.println("</ul>");
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
